package com.tech.koufu.ui.activity.menu;

 
import com.tech.koufu.MyApplication;
import com.tech.koufu.R; 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; 
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.RelativeLayout;
import android.widget.TextView; 

/**
 * 
 * @ClassName: MyTradeActivity
 * @Description: 我的交易
 * @author 123
 * @date 2015年4月4日 下午2:05:53
 */
public class MyTradeActivity extends FragmentActivity implements OnClickListener {
	
	private RelativeLayout rl_tab_index,rl_tab_second;
	private TextView txt_tab_index,txt_tab_his;
	private View v_tab_index,v_tab_second;
	private String userid,groupid,digitalid;
	private MyApplication myApp;
	// 默认选中第一个tab
		private int index = 1;
		// fragment管理类
		private FragmentManager fragmentManager;
		// 三个fragment
		private Fragment tab1Fragment, tab2Fragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_my_trade);
		myApp = (MyApplication) getApplication();
		initView();
		initData();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		userid = getIntent().getStringExtra("userid");
		groupid = getIntent().getStringExtra("groupid");
		digitalid = getIntent().getStringExtra("digitalid");
		findViewById(R.id.img_callback).setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.tv_title);
//		if(userid!=null && myApp.getUserid().equals(userid)){
//			title.setText(R.string.menu_mytransaction);
//		}else{
			title.setText("历史成交");
//		}
			initTab();
	}

	private void initTab() {
		rl_tab_index = (RelativeLayout)findViewById(R.id.rl_tab_index);
		rl_tab_second = (RelativeLayout)findViewById(R.id.rl_tab_second);
		txt_tab_index = (TextView )findViewById(R.id.txt_tab_index);
		txt_tab_his = (TextView)findViewById(R.id.txt_trade_his);
		v_tab_index = (View)findViewById(R.id.v_tab_index);
		v_tab_second = (View)findViewById(R.id.v_tab_second);
		rl_tab_index.setOnClickListener(this);
		rl_tab_second.setOnClickListener(this);
	}
	 
	private void initData(){
		fragmentManager = getSupportFragmentManager();
		setDefaultFragment();
	}
	private void resetTabLine(){ 
		v_tab_index.setVisibility(View.INVISIBLE);
		v_tab_second.setVisibility(View.INVISIBLE); 
		
		txt_tab_index.setTextColor(getResources().getColor(R.color.textColorGray_888888));
		txt_tab_his.setTextColor(getResources().getColor(R.color.textColorGray_888888)); 
	}
	private void showTabLine(View v){
		v.setVisibility(View.VISIBLE);
	}
	private void changeTextColor(TextView tv,int colors){
		tv.setTextColor(getResources().getColor(colors));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_callback:
			this.finish();
			break;
		case R.id.rl_tab_index:
			if(v_tab_index.getVisibility() != View.VISIBLE){  
				resetTabLine();
				showTabLine(v_tab_index);
				changeTextColor(txt_tab_index, R.color.kfColorRed);
			}
			if (tab1Fragment == null) {
				tab1Fragment =TradeDateFragment.newInstance(index, userid, groupid,digitalid);
			}
			replaceFragment(tab1Fragment); 
			index = 1;
			break;
		case R.id.rl_tab_second:
			if(v_tab_second.getVisibility() != View.VISIBLE){  
				resetTabLine();
				showTabLine(v_tab_second);
				changeTextColor(txt_tab_his, R.color.kfColorRed);
			}
			if (tab2Fragment == null) {
				tab2Fragment =TradeHistoryFragment.newInstance(index, userid, groupid,digitalid);
			}
			replaceFragment(tab2Fragment); 
			index = 2;
			break;
		default:
			break;
		}
	}
	/**
	 * 设置默认显示的fragment
	 */
	private void setDefaultFragment() {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		tab1Fragment =TradeDateFragment.newInstance(index, userid, groupid,digitalid);
		transaction.replace(R.id.content_layout, tab1Fragment);
		transaction.commit();
	}

	/**
	 *切换fragment
	 * @param newFragment
	 */
	private void replaceFragment(Fragment newFragment) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (!newFragment.isAdded()) {
			transaction.replace(R.id.content_layout, newFragment);
			transaction.commit();
		} else {
			transaction.show(newFragment);
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) { 
		if(keyCode == KeyEvent.KEYCODE_BACK){
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
